"""
  Copyright 2008 Texas A&M University Licensed under the
  Educational Community License, Version 2.0 (the "License"); you may
  not use this file except in compliance with the License. You may
  obtain a copy of the License at
  
  http://www.osedu.org/licenses/ECL-2.0

  Unless required by applicable law or agreed to in writing,
  software distributed under the License is distributed on an "AS IS"
  BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
  or implied. See the License for the specific language governing
  permissions and limitations under the License.
"""

import unittest
from xml.dom import minidom

from lxml import etree

from meercat.base import Metadata
from meercat.mapreduce.mappers import marc



marc21_bib_rec = u"01142cam  2200301 a 4500001001300000003000400013005001700017" \
                 "0080041000340100017000750200025000920400018001170420009001350" \
                 "5000260014408200160017010000320018624500860021825000120030426" \
                 "0005200316300004900368500004000417520022800457650003300685650" \
                 "003300718650002400751650002100775650002300796700002100819   " \
                 "92005291 DLC19930521155141.9920219s1993    caua   j      00" \
                 "0 0 eng    a   92005291   a0152038655 :c$15.95  aDLCcDLC" \
                 "dDLC  alcac00aPS3537.A618bA88 199300a811/.522201 aSandb" \
                 "urg, Carl,d1878-1967.10aArithmetic /cCarl Sandburg ; illust" \
                 "rated as an anamorphic adventure by Ted Rand.  a1st ed.  aS" \
                 "an Diego :bHarcourt Brace Jovanovich,cc1993.  a1 v. (unpage" \
                 "d) :bill. (some col.) ;c26 cm.  aOne Mylar sheet included i" \
                 "n pocket.  aA poem about numbers and their characteristics. " \
                 "Features anamorphic, or distorted, drawings which can be rest" \
                 "ored to normal by viewing from a particular angle or by viewi" \
                 "ng the image's reflection in the provided Mylar cone. 0aArit" \
                 "hmeticxJuvenile poetry. 0aChildren's poetry, American. 1aA" \
                 "rithmeticxPoetry. 1aAmerican poetry. 1aVisual perception.1" \
                 " aRand, Ted,eill."

marcxml_bib_string = u"""
<record>
<leader>01142cam  2200301 a 4500</leader>
<controlfield tag="001">   92005291 </controlfield>
<controlfield tag="003">DLC</controlfield>
<controlfield tag="005">19930521155141.9</controlfield>
<controlfield tag="008">920219s1993    caua   j      000 0 eng  </controlfield>
<datafield tag="010" ind1=" " ind2=" ">
<subfield code="a">   92005291 </subfield>
</datafield>
<datafield tag="020" ind1=" " ind2=" ">
<subfield code="a">0152038655 :</subfield>
<subfield code="c">$15.95</subfield>
</datafield>
<datafield tag="040" ind1=" " ind2=" ">
<subfield code="a">DLC</subfield>
<subfield code="c">DLC</subfield>
<subfield code="d">DLC</subfield>
</datafield>
<datafield tag="042" ind1=" " ind2=" ">
<subfield code="a">lcac</subfield>
</datafield>
<datafield tag="050" ind1="0" ind2="0">
<subfield code="a">PS3537.A618</subfield>
<subfield code="b">A88 1993</subfield>
</datafield>
<datafield tag="082" ind1="0" ind2="0">
<subfield code="a">811/.52</subfield>
<subfield code="2">20</subfield>
</datafield>
<datafield tag="100" ind1="1" ind2=" ">
<subfield code="a">Sandburg, Carl,</subfield>
<subfield code="d">1878-1967.</subfield>
</datafield>
<datafield tag="245" ind1="1" ind2="0">
<subfield code="a">Arithmetic /</subfield>
<subfield code="c">
Carl Sandburg ; illustrated as an anamorphic adventure by Ted Rand.
</subfield>
</datafield>
<datafield tag="250" ind1=" " ind2=" ">
<subfield code="a">1st ed.</subfield>
</datafield>
<datafield tag="260" ind1=" " ind2=" ">
<subfield code="a">San Diego :</subfield>
<subfield code="b">Harcourt Brace Jovanovich,</subfield>
<subfield code="c">c1993.</subfield>
</datafield>
<datafield tag="300" ind1=" " ind2=" ">
<subfield code="a">1 v. (unpaged) :</subfield>
<subfield code="b">ill. (some col.) ;</subfield>
<subfield code="c">26 cm.</subfield>
</datafield>
<datafield tag="500" ind1=" " ind2=" ">
<subfield code="a">One Mylar sheet included in pocket.</subfield>
</datafield>
<datafield tag="520" ind1=" " ind2=" ">
<subfield code="a">
A poem about numbers and their characteristics. Features anamorphic, or distorted, drawings which can be restored to normal by viewing from a particular angle or by viewing the image's reflection in the provided Mylar cone.
</subfield>
</datafield>
<datafield tag="650" ind1=" " ind2="0">
<subfield code="a">Arithmetic</subfield>
<subfield code="x">Juvenile poetry.</subfield>
</datafield>
<datafield tag="650" ind1=" " ind2="0">
<subfield code="a">Children's poetry, American.</subfield>
</datafield>
<datafield tag="650" ind1=" " ind2="1">
<subfield code="a">Arithmetic</subfield>
<subfield code="x">Poetry.</subfield>
</datafield>
<datafield tag="650" ind1=" " ind2="1">
<subfield code="a">American poetry.</subfield>
</datafield>
<datafield tag="650" ind1=" " ind2="1">
<subfield code="a">Visual perception.</subfield>
</datafield>
<datafield tag="700" ind1="1" ind2=" ">
<subfield code="a">Rand, Ted,</subfield>
<subfield code="e">ill.</subfield>
</datafield>
</record>
""".replace(u'\n',u'')

class BaseTest(unittest.TestCase):
    def setUp(self):
        self.mapper = marc.Marc21ToMarcXmlMapper()
    
    def testMarcBibliographic(self):
        md = Metadata(source='blah',format='marc21.bibliographic',
                      value=marc21_bib_rec)
        
        result = self.mapper.map(md)
        
        self.assertEqual(minidom.parseString(result.getValue()).toxml(),
                         minidom.parseString(marcxml_bib_string).toxml())

def test_suite():
    suite = unittest.TestSuite()
    suite.addTest(unittest.TestLoader().loadTestsFromTestCase(BaseTest))
    return suite

if __name__ == '__main__':
    unittest.TextTestRunner(verbosity=2).run(test_suite())